﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Pouer.Resources;
using Microsoft.Live;
using Microsoft.Live.Controls;

namespace Pouer
{
    public partial class MainPage : PhoneApplicationPage
    {
        private LiveConnectClient client;
        
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Código de ejemplo para traducir ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        // Código de ejemplo para compilar una ApplicationBar traducida
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Establecer ApplicationBar de la página en una nueva instancia de ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Crear un nuevo botón y establecer el valor de texto en la cadena traducida de AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Crear un nuevo elemento de menú con la cadena traducida de AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
        private void btnSignin_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
        {
            if (e.Status == LiveConnectSessionStatus.Connected)
            {
                client = new LiveConnectClient(e.Session);
                App.Current.LiveSession = e.Session;
                this.txtLoginResult.Text = "Signed in.";
                this.txtWelcome.Visibility = System.Windows.Visibility.Visible;
                this.btnShowContent.Visibility = System.Windows.Visibility.Visible;
                this.btnAddFile.Visibility = System.Windows.Visibility.Visible;

                client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(OnGetCompleted);
                client.GetAsync("me", null);
            }
            else
            {
                this.txtLoginResult.Text = "Not signed in.";
                this.txtWelcome.Visibility = System.Windows.Visibility.Collapsed;
                client = null;
            }
        }

        void OnGetCompleted(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.ContainsKey("first_name") &&
                    e.Result.ContainsKey("last_name"))
                {
                    if (e.Result["first_name"] != null &&
                        e.Result["last_name"] != null)
                    {
                        this.txtWelcome.Text =
                            "Hello, " +
                            e.Result["first_name"].ToString() + " " +
                            e.Result["last_name"].ToString() + "!";
                    }
                }
                else
                {
                    txtWelcome.Text = "Hello, signed-in user!";
                }
            }
            else
            {
                txtWelcome.Text = "Error calling API: " +
                    e.Error.ToString();
            }
        }
    
    
    }
}